const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({
  data: {
    tabs: '',
    uid: ''
  },
  onLoad(options) {
    if (options.uid) {
      this.data.uid = options.uid;

      wx.setStorageSync('weapp_uid', this.data.uid);
    }
    var that = this;
    var p = {
      m: 'new_index',
    }
    wx.request({
      url: 'https://weapp.whwangdoudou.cn/weapp.php',
      data: p,
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          tabs: res.data.result
        });
      }
    })
  },

  tapId(e) {
    let weappid = e.currentTarget.dataset.weappid;
    let that = this;
    let timestamp = parseInt(new Date().getTime() / 1000);
    let content = {
      userinfo: wx.getStorageSync('_userinfo'),
      device_info: wx.getStorageSync('device_info')
    };
    content = JSON.stringify(content);

    let guest_time_start = parseInt(new Date().getTime() / 1000);
    wx.setStorageSync('guest_time_start_' + that.data.uid + '_' + weappid, guest_time_start);
    wx.setStorageSync('weappid', weappid);

    let p = {
      m: 'invoke',
      type: 'guest',
      content: content,
      uid: that.data.uid,
      weappid: weappid
    };
    post(p).then(res => {
      //console.log(res);
    });
  },

  onReady: function () {
    wx.getStorageInfo({
      success: function (res) {
        //console.log(res.keys)
        for (var i = 0; i < res.keys.length; i++) {
          var key = res.keys[i];
          //console.log(key);
          if (0 == key.indexOf('guest_time_start_')) {
            wx.removeStorageSync(key);
          } else if (0 == key.indexOf('guest_time_end_')) {
            wx.removeStorageSync(key);
          } else if (0 == key.indexOf('weappid')) {
            wx.removeStorageSync(key);
          }
        }
      }
    });
  },

  onShow() {
    let that = this;
    let weappid = wx.getStorageSync('weappid');
    let guest_time_start = wx.getStorageSync('guest_time_start_' + that.data.uid + '_' + weappid)
    if (that.data.uid && weappid) {
      if (guest_time_start && guest_time_start > 0) {
        let guest_time_end = parseInt(new Date().getTime() / 1000);
        wx.setStorageSync('guest_time_end_' + that.data.uid + '_' + weappid, guest_time_end);
        let guest_time = guest_time_end - guest_time_start;
        let content = {
          userinfo: wx.getStorageSync('_userinfo'),
          device_info: wx.getStorageSync('device_info'),
          guest_time: guest_time
        };
        content = JSON.stringify(content);
        let p = {
          m: 'invoke',
          type: 'guest_end',
          content: content,
          uid: that.data.uid,
          weappid: weappid
        };
        post(p).then(res => {
          wx.removeStorageSync('weappid');
          wx.removeStorageSync('guest_time_start_' + that.data.uid + '_' + weappid);
          wx.removeStorageSync('guest_time_end_' + that.data.uid + '_' + weappid);
          if (guest_time < 30) {

          } else {

          }
        })
      }
    }
  },
  submitFormId(e) {
    let formId = e.detail.formId;
    let button = e.detail.target.dataset.name;
    let page = getCurrentPageUrlWithArgs();
    let that = this;
    let p = {
      m: 'colform',
      uid: that.data.uid,
      openid: wx.getStorageSync('weapp_openid'),
      formId: formId
    };
    if (page && page.length > 0) {
      p.page = page;
    }
    if (button && button.length > 0) {
      p.button = button;
    }
    //console.log(p);
    if (that.data.uid) {
      post(p).then(res => {
      })
    }

  }
})